﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Classified.Data.Infrastructure;
using Classified.Data.Repositories;
using Classified.Domain.Entities;

namespace Classified.Service
{
    public interface ISecurityService
    {
        IEnumerable<User> GetUsers();
        User GetUserById(int id);
        User GetUser(string userName);
        User CheckLogin(string userName,string password);
        void CreateUser(User user);
        void DeleteUser(string userName);
        void DeleteUser(int id);
        User UpdateUser(User user);
        User GetUserByName(string username);
        IEnumerable<Role> Roles();
        Role GetRole(string roleName);
        void CreateRole(Role role);
        void DeleteRole(string roleName);
        bool IsUserNameAvailabel(string name);
        void Assignlocations(int id, int location);
        void AssignUsers(int id, int userid);
        void Save();
        void AssignAttributes(int id, List<int> attributes);
        void AssignRole(int id, List<string> roleNames);
        bool IsEmailIDAvailabel(string EmailId);
        User GetUserByEmailId(string emailID);
    }
    public class SecurityService : ISecurityService
    {
        private readonly IUserRepository _userRepository;
        private readonly IRoleRepository _roleRepository;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IClassifiedRepository _classifiedRepository;
        private readonly IClassifiedAdCategoryRepository _classifiedAdCategoryRepository;
        public SecurityService(IUserRepository userRepository, IRoleRepository roleRepository, IUnitOfWork unitOfWork, IClassifiedRepository classifiedRepository,
            IClassifiedAdCategoryRepository classifiedAdCategoryRepository)
        {
            this._userRepository = userRepository;
            this._roleRepository = roleRepository;
            this._unitOfWork = unitOfWork;
            this._classifiedRepository = classifiedRepository;
            this._classifiedAdCategoryRepository = classifiedAdCategoryRepository;

        }
        public IEnumerable<User> GetUsers()
        {
            var users = _userRepository.GetAll();
            return users;
        }

       
        /// <summary>
        /// Get User by User Name
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public User GetUser(string userName)
        {
            var user = _userRepository.GetById(userName);
            return user;
        }
        public User GetUserByName(string username)
        {
            var user = _userRepository.GetById(x=>x.UserName == username);
            return user;
        }
        public User GetUserByEmailId(string emailID)
        {
            var user = _userRepository.GetById(x => x.Email == emailID);
            return user;
        }
        public User GetUserById(int id)
        {
            var user = _userRepository.GetById(id);
            return user;
        }

        public User CheckLogin(string userName, string password)
        {

            var roles = _roleRepository.GetAll().ToList();
            //  var user = this.Filter(x => x.UserName == logIn.UserName && x.Password==logIn.Password &&x.IsActive).FirstOrDefault();
            var user = _userRepository.Get(x => x.UserName == userName &&x.Password==password);
            if (user == null)
                return null;

            //var password = CryptorEngine.Encrypt(logIn.Password, true);
            //if (user.Password == password)
            //    return user;
            return user;
        }

        public void CreateUser(User user)
        {
            _userRepository.Add(user);
           
        }

        public void DeleteUser(string userName)
        {
            var user = GetUser(userName);
            _userRepository.Delete(user);
           
        }

        public void DeleteUser(int id)
        {
            var user = GetUserById(id);
            if(user!=null)
            _userRepository.Delete(user);
        }

        public User UpdateUser(User user)
        {
            _userRepository.Update(user);
            return user;
        }

        public IEnumerable<Role> Roles()
        {
            var roles = _roleRepository.GetAll();
            return roles;
        }

        public Role GetRole(string roleName)
        {
            var role = _roleRepository.GetById(roleName);
            return role;
        }

        public void CreateRole(Role role)
        {
            _roleRepository.Add(role);
            Save();
        }

        public void DeleteRole(string roleName)
        {
            var role = GetRole(roleName);
            _roleRepository.Delete(role);
           
        }

        public bool IsUserNameAvailabel(string name)
        {
            var user = _userRepository.GetMany(x => x.UserName == name).Any();
            return !user;
        }
        public bool IsEmailIDAvailabel(string EmailId)
        {
            var user = _userRepository.GetMany(x => x.Email == EmailId).Any();
            return !user;
        }

        public void AssignRole(int id, List<string> roleNames)
        {
            _userRepository.AssignRole(id, roleNames);
           
        }
        public void Assignlocations(int id, int location)
        {
           _classifiedRepository.Assignlocations(id, location);

        }
        public void AssignUsers(int id, int userid)
        {
            _classifiedRepository.AssignUser(id, userid);

        }
        public void AssignAttributes(int id, List<int> attributes)
        {
            _classifiedRepository.AssignAttributeVaues(id, attributes);

        }
        public void Save()
        {
           
            _unitOfWork.Commit();
        }
    }
}
